// pages/star/star.js
var utils = require('../../utils/utils.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      pics:"../../img/TX/bg.png",
      user:{
        "head":"../img/TX/001.jpg",
        "name":"工具人"
      },
      starHead:"https://www.304cloud.top:4321",
      ifdianzan:"qian"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      wx.setNavigationBarTitle({
        title: '星辰'
      })
      // this.getStar();
      // 查看是否授权
      let userID = wx.getStorageSync('openid')
      this.setData({
        userID:userID
      })
      wx.getUserInfo({
        success:res=> {
          console.log(res.userInfo)
          let uinfo = res.userInfo;
          this.setData({
            myHeadImage:uinfo.avatarUrl,
            myName:uinfo.nickName
          })
        }
      })
      this.getDongtai();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    onPullDownRefresh:function()
  {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    
    //模拟加载
    this.getDongtai();
  },
    dianzan:function(e){
      console.log(e.currentTarget.id)
      wx.request({
        url : "https://www.304cloud.top:4321/like",
        method: "GET",
        data: {
          user_id:this.data.userID,
          article_id:e.currentTarget.dataset.id
        },
        header: {
          "Content-Type": "application/json"
        },
        success: (res)=>{
          console.log(res.data);
          if(res.data == "ok"){
            this.setData({
              ifdianzan:true
            })
            wx.showToast({
              title: '点赞成功',
              icon: 'none',
              duration: 1000
            });
            this.onLoad();
          }else{
            this.setData({
              ifdianzan:false
            })
            wx.showToast({
              title: '已取消点赞',
              icon: 'none',
              duration: 1000
            });
            this.onLoad();
          }
        }
      })
      
    },
    getPinglun:function(e){
      console.log(e.detail.value)
      this.setData({
        pinglunValue:e.detail.value
      })
    },
    pinglunSubmit:function(){
      console.log("评论")
      this.setData({
          pinglun_txt:this.data.pinglunValue
      });
      console.log(this.data.pinglun_txt)
      wx.request({
      url : "https://www.304cloud.top:4321/comment",
      method: "POST",
      data: {
          user_id:this.data.userID,
          comment_txt:this.data.pinglun_txt, 
          article_id:this.data.article_id,
          comment_time:utils.formatTime(new Date()),
      },
      header: {
          "Content-Type": "application/json"
      },
      success: function (res) {
          console.log(res);
          wx.showToast({
          title: '评论成功！',
          icon: 'success',
          duration: 2000
          })
      },
      })
  },
    getDongtai:function(){
      let star = [];
      let starTxt = [];
      let ifLike = [];
      wx.request({
        url : "https://www.304cloud.top:4321/getarticles",
        method: "GET",
        data: {
          user_id:this.data.userID
        },
        header: {
          "Content-Type": "application/json"
        },
        success: (res)=> {
          console.log(res.data.data);
          this.setData({
            star:res.data.data.sort(this.compareTime('article_time')),
          })
          console.log(this.data.star)
          for(var i=0;i<this.data.star.length;i++){
            starTxt.push(this.data.star[i].article_txt.replace(/。/g,'\n').replace(/，/g,'\n'))
            ifLike.push(this.data.star[i].iflike)
          }
          this.setData({
            starTxt:starTxt,
            ifLike:ifLike,
            starHead:"https://www.304cloud.top:4321"
          })
        }
      })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
      }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    choseImg:function(e){
      let that=this;
      let pics = that.data.pics;
      wx.chooseImage({
        count:1,
        sizeType: ['original', 'compressed'], 
        sourceType: ['album', 'camera'],
        success: function(res) {
        let imgSrc = res.tempFilePaths;
        pics=imgSrc;
          that.setData({
            pics: pics
          })
        },
      })
    },
    getUserInfo : function(e) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    },
    compareTime: function(property) {
      return function(a, b) {
        var value1 = Date.parse(a[property]);
        var value2 = Date.parse(b[property]);
        return value2 - value1;
      }
    },
    goToUserDetail:function(e){
      let userId=e.currentTarget.dataset.id;
      if(userId == this.data.userID){
        wx.switchTab({
          url: '/pages/my/my',
        })
      }else{
        wx.navigateTo({
          url: '/pages/user/user?id=' +userId
        })
      }
      console.log(userId)
    },
    goToTieziDetail:function(e){
      let tieziId=e.currentTarget.dataset.id;
      console.log(tieziId)
      wx.navigateTo({
          url: '/pages/tiezi/tiezi?id=' +tieziId
      })
    },
})